home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ezhint.exe / STRINGS.CPP < prev    next >
C/C++ Source or Header  |  1992-12-09  |  3KB  |  188 lines

  1. #define Uses_ifpstream
  2. #define Uses_ofpstream
  3. #define Uses_TStringList
  4. #define Uses_TStrListMaker
  5. #define Uses_Strings
  6. #include <tv.h>
  7. #include "strings.h"
  8. #include <mem.h>
  9. #include <string.h>
  10. #include <io.h>
  11. #include <share.h>
  12. #include <fcntl.h>
  13.  
  14. __link(RStringList)
  15.  
  16. Strings::Strings() :
  17.         items(0),
  18.         count(0),
  19.         slist(0),
  20.         is(0)
  21.     {
  22.     }
  23.  
  24. Strings::Strings( Strings& s ) :
  25.         items(0),
  26.         count(0),
  27.         slist(0),
  28.         is(0)
  29.     {
  30.     if( s.items )    // s contains array of pointers to StrRef.
  31.         {
  32.         items = new StrRef *[s.count];
  33.         if( items == 0 )
  34.             return;
  35.         memcpy( items, s.items, s.count*sizeof(StrRef *) );
  36.         count = s.count;
  37.         }
  38.     else if( s.slist )    // s uses string resource file.
  39.         load( s.fname );
  40.     }
  41.  
  42. Strings::~Strings()
  43.     {
  44.     clear();
  45.     }
  46.  
  47. char *Strings::get( ushort id )
  48.     {
  49.     int n;
  50.  
  51.     if( items )        // uses StrRef pointer array.
  52.         {
  53.         if( search(id, n) )
  54.             return items[n]->str;
  55.         else
  56.             return 0;
  57.         }
  58.     else if( slist )    // uses string resource file.
  59.         {
  60.         slist->get( buf, id );
  61.         if( *buf )
  62.             return buf;
  63.         else
  64.             return 0;
  65.         }
  66.     else        // empty.
  67.         return 0;
  68.     }
  69.  
  70. void Strings::clear()
  71.     {
  72.     if( items )
  73.         delete items;
  74.     items = 0;
  75.     count = 0;
  76.  
  77.     if( slist )
  78.         delete slist;
  79.     slist = 0;
  80.     if( is )
  81.         delete is;
  82.     is = 0;
  83.     }
  84.  
  85. Boolean Strings::load( StrRef *arg )
  86.     {
  87.     clear();
  88.     if( arg == 0 )    return False;
  89.  
  90.     int limit, u, n;
  91.     for( limit = 0; arg[limit].id != srNull; limit++ );
  92.     items = new StrRef *[limit];
  93.     if( items == 0 )
  94.         return False;
  95.     for( u = 0; u < limit; u++ )
  96.         {
  97.         search( arg[u].id, n );
  98.         if( n < count )
  99.             memmove( items+n+1, items+n, (count-n)*sizeof(StrRef *) );
  100.         items[n] = &arg[u];
  101.         count++;
  102.         }
  103.     return True;
  104.     }
  105.  
  106. Boolean Strings::load( char *fspec )
  107.     {
  108.     clear();
  109.     strcpy( fname, fspec );
  110.     int handle = open( fname, O_RDONLY | SH_DENYWR | O_BINARY );
  111.     if( handle < 0 )
  112.         return False;
  113.     is = new ifpstream;
  114.     if( is == 0 )
  115.         {
  116.         close(handle);
  117.         return False;
  118.         }
  119.     is->attach(handle);
  120.     if( !(*is) )
  121.         {
  122.         close(handle);
  123.         delete is;
  124.         return False;
  125.         }
  126.     *is >> slist;
  127.     return True;
  128.     }
  129.  
  130. Boolean Strings::store( char *fspec )
  131.     {
  132.     if( items == 0 )
  133.         return False;
  134.  
  135.     long len, i, n;
  136.  
  137.     len = 0L;
  138.     for( i = 0; i < count; i++ )
  139.         len += strlen(items[i]->str)+1;
  140.     len += 10; // buffer
  141.     if( len > 0xFFFFL )
  142.         return False;    // too big
  143.     TStrListMaker *lm = new TStrListMaker( (ushort)len, count+2 );
  144.     if( !lm )    return False;
  145.     for( i = 0; i < count;  i++ )
  146.         lm->put( items[i]->id, items[i]->str );
  147.  
  148.     ofpstream os( fspec );
  149.     if( !os )
  150.         {
  151.         os.close();
  152.         delete lm;
  153.         return False;
  154.         }
  155.     os << lm;
  156.     os.close();
  157.     delete lm;
  158.     return True;
  159.     }
  160.  
  161. Boolean Strings::search( ushort id, int& pos )
  162.     {
  163.     Boolean found = False;
  164.     if( !count )
  165.         pos = 0;
  166.     else
  167.         {
  168.         int l = 0;
  169.         int h = count - 1;
  170.         while( l <= h )
  171.             {
  172.             int i = (l +  h) >> 1;
  173.             if( items[i]->id < id )
  174.                 l = i + 1;
  175.             else if( items[i]->id > id )
  176.                 h = i - 1;
  177.             else
  178.                 {
  179.                 found = True;
  180.                 l = i;
  181.                 h = i-1;
  182.                 }
  183.             }
  184.         pos = l;
  185.         }
  186.     return found;
  187.     }
  188.